import { loadTaskLogsApi } from '@/api';
import { rerunTask } from './api';
import { message } from '@/utils/util';
import { export_json_to_excel, formatJson } from '@/utils/vendor/Export2Excel';

export default {
  name: 'task-log-list-table',
  props: {
    data: {
      type: Array,
      default: () => [],
    },
    total: {
      type: Number,
      default: 0,
    },
    pageNum: {
      type: Number,
      default: 1,
    },
    pageSize: {
      type: Number,
      default: 10,
    },
    loading: {
      type: Boolean,
      default: false,
    },
  },
  computed: {
    currentPage() {
      return this.pageNum;
    },
  },
  data() {
    return {
      rollBacking: false,
    };
  },
  methods: {
    onPageSizeChange(pageSize) {
      this.$emit('pageSizeChange', pageSize);
    },
    onPageNumChange(pageNum) {
      this.$emit('pageNumChange', pageNum);
    },

    onOpenStoreDetailModal(row) {
      this.$emit('onOpenEditModal', row);
    },
    //下载错误文件
    async downloadError(row) {
      try {
        const result = await loadTaskLogsApi(row.id);
        if (result && result.length > 0) {
          const tHeader = ['渠道编码', '商品编码', '原因'];
          const filterVal = ['channelCode', 'goodsCode', 'reason'];
          const data = formatJson(filterVal, result);
          const date = new Date();
          const day = date.getDay() + '月' + date.getDate() + '日';
          export_json_to_excel(tHeader, data, '商品调价错误日志(' + day + ')-' + row.id);
        } else {
          message('提示', '没有可导出的数据', 'warning');
        }
      } catch (e) {
        message('提示', '下载错误文件异常', 'error');
      }
    },

    //重新执行
    async rollBack(row) {
      if (this.rollBacking) {
        message('提示', '请勿重复执行', 'error');
        return;
      }
      this.rollBacking = true;
      try {
        await rerunTask(row.id);
        message('提示', '重新执行成功', 'success');
      } catch (e) {
        message('提示', '重新执行失败', 'error');
      }
      setTimeout(() => {
        this.rollBacking = false;
      }, 5000);
    },
  },
  mounted() {},
};
